package com.tesis_2.DA;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



import com.tesis_2.MO.MORestaurant;


public class DAORestaurants {
	

	
	public static ArrayList<MORestaurant> getRestaurants() throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		ArrayList<MORestaurant> lsRest = new ArrayList<MORestaurant>();
		
	    try{
	    	String ulr = DAOUtils.getServer()+ "/restaurants.json";
	    	JSONArray rests = (new JSONReaderArray().execute(DAOUtils.getServer()+ "/restaurants.json")).get();
	    	
	    	if (rests == null) return lsRest;
	       	    	
	    	for (int i = 0; i < rests.length(); i++) {
	    		JSONObject jsonrest = rests.getJSONObject(i);
	    	    jsonrest = jsonrest.getJSONObject("restaurant");
	    	    //obtengo el restaurante
	    	    MORestaurant objRest = new MORestaurant();
	    	    //entro al objeto y leo los atributos
	    	    objRest.setName(jsonrest.getString("name"));
	    	    objRest.setId(jsonrest.getInt("id")) ;
	    	    objRest.setDescr(jsonrest.getString("descr"));
	    	    objRest.setCuisine(jsonrest.getString("cuisine"));
	    	    objRest.setEmail(jsonrest.getString("email"));
	    	    objRest.setPhone(jsonrest.getString("phone"));
	    	    objRest.setPhoto(jsonrest.getString("photo_file_name"));
	    	    objRest.setScore( (float)(jsonrest.getDouble("score")) );
	    	    objRest.setNcomments(jsonrest.getInt("ncomments"));
	    	    objRest.setUpdated_at(DAOUtils.parseDateIso(jsonrest.getString("updated_at")));
	    	    objRest.setCreated_at(DAOUtils.parseDateIso(jsonrest.getString("created_at")));
	    	    
	    	    //{"restaurant":{"created_at":"2012-12-08T21:05:18Z","cuisine":"Japanese",
	    	    //"descr":"Pokemon Food","email":"poke@ball.com","id":1,"name":"Schyter's Sushi",
	    	    //"ncomments":0,"phone":"999-999-999","photo_content_type":"image/png","photo_file_name":"poke.png"
	    	    //,"photo_file_size":92456,"photo_updated_at":"2012-12-11T00:45:16Z","score":0.0,"updated_at":"2012-12-11T00:45:16Z"}}
	    	    
	    	    lsRest.add(objRest);
	    	}
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsRest;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsRest;
		}
		return lsRest;
		
	}
	
}
